﻿using QFramework;
using UnityEngine;

namespace ProjectMyFarmAndShe
{
    public class AddFirstSeries
    {
        public static void AddChallenge(ChallengeSeries series)
        {
            var emailSystem = Global.Interface.GetSystem<IEmailSystem>();
            var toolBarSystem = Global.Interface.GetSystem<IToolbarSystem>();

            var firstChallenge = new ChallengeData()
                .WithKey("未曾见面的女生的第一个委托")
                .WithPublisher(Global.Heroine.Value)
                .WithDescription("为一个女生提供三十个土豆和二十个萝卜。")
                .WithUnlock((self) => true)
                .WithCondition(() =>
                {
                    if (Global.PotatoCount.Value >= 30 && Global.TurnipCount.Value >= 20)
                    {
                        return true;
                    }

                    return false;
                })
                .WithFinishedAction((self) =>
                {
                    Global.PotatoCount.Value -= 30;
                    Global.TurnipCount.Value -= 20;
                    toolBarSystem.SubToolbarData("Potato", 30);
                    toolBarSystem.SubToolbarData("Turnip", 20);
                    Global.Coin.Value += 100;

                    var emailData = new EmailData()
                        .WithTitle($"{self.Publisher}第{self.FinishedDay}天的感谢信")
                        .WithPublisher(self.Publisher)
                        .WithInfo("很感谢您对我的帮助,家里的粮食已经不够继续生活下去了,如果没有您的土豆和萝卜我不知道该怎么活下去,谢谢您。")
                        .WithSendDay(self.FinishedDay + 1)
                        .WithRead(false);

                    emailSystem.AddEmail(emailData);

                    MessagePanel.Default.AddMessage($"{self.Key}完成");
                });

            var secondChallenge = new ChallengeData()
                .WithKey("未曾见面的女生的第二个委托")
                .WithPublisher(Global.Heroine.Value)
                .WithDescription("为一个女生提供三十个土豆和二十个萝卜。")
                .WithUnlock((self) =>
                {
                    if (firstChallenge.Finished && Global.Day.Value >= firstChallenge.FinishedDay + 2)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.PotatoCount.Value >= 30 && Global.TurnipCount.Value >= 20)
                    {
                        return true;
                    }

                    return false;
                })
                .WithFinishedAction((self) =>
                {
                    Global.PotatoCount.Value -= 30;
                    Global.TurnipCount.Value -= 20;

                    toolBarSystem.SubToolbarData("Potato", 30);
                    toolBarSystem.SubToolbarData("Turnip", 20);

                    Global.Coin.Value += 100;

                    var emailData = new EmailData()
                        .WithTitle($"{self.Publisher}第{self.FinishedDay}天的感谢信")
                        .WithPublisher(self.Publisher)
                        .WithInfo("谢谢您又一次帮助了我,因为家里的原因让工会对我有点关照,但这不是麻烦您的理由,自从上次您帮助我了之后,我之前种植的土豆和稻米也快收获了," +
                                  "如果有机会的话,我想将稻米带给您尝尝。虽然可能质量不如您种出来的,但希望您不要嫌弃")
                        .WithSendDay(self.FinishedDay + 1)
                        .WithRead(false);

                    emailSystem.AddEmail(emailData);
                    
                    MessagePanel.Default.AddMessage($"{self.Key}完成");
                });

            var thirdChallenge = new ChallengeData()
                .WithKey("未曾见面的女生的第三个委托")
                .WithPublisher(Global.Heroine.Value)
                .WithDescription("为一个女生提供二十个葡萄和十个粉色萝卜。")
                .WithUnlock((self) =>
                {
                    if (secondChallenge.Finished && Global.Day.Value >= secondChallenge.FinishedDay + 3)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.PinkRadishCount.Value >= 10 && Global.GrapeCount.Value >= 20)
                    {
                        return true;
                    }

                    return false;
                })
                .WithFinishedAction((self) =>
                {
                    Global.PinkRadishCount.Value--;
                    Global.GrapeCount.Value--;

                    toolBarSystem.SubToolbarData("PinkRadish", 10);
                    toolBarSystem.SubToolbarData("Grape", 20);

                    Global.Coin.Value += 150;

                    var emailData = new EmailData()
                        .WithTitle($"{self.Publisher}第{self.FinishedDay}天的感谢信")
                        .WithPublisher(self.Publisher)
                        .WithInfo("因为大雨天气,导致稻米全部被淹死了,现在镇上的工会都在进行补救工作,这也许是上天给我的惩罚吧,不过我不会放弃的,等补救工作完成之后我就会再继续种植。" +
                                  "我听了工会人员对您的评价后,我也希望我能成为您这样的勤奋努力的人。" +
                                  "忘记告诉您我的名字了,对帮助我的人却没有告诉名字是我的失职,我的名字叫做李菀怡。" +
                                  "感谢这么久以来您对我的帮助,真的非常感谢您。")
                        .WithSendDay(self.FinishedDay + 1)
                        .WithRead(false);

                    emailSystem.AddEmail(emailData);

                    Global.Heroine.Value = "李菀怡";
                    
                    MessagePanel.Default.AddMessage($"{self.Key}完成");
                });

            var fourthChallenge = new ChallengeData()
                .WithKey("未曾见面的女生的第三个委托")
                .WithPublisher(Global.Heroine.Value)
                .WithDescription("为一个女生提供三十个茄子和二十个蓝莓。")
                .WithUnlock((self) =>
                {
                    if (thirdChallenge.Finished && Global.Day.Value >= thirdChallenge.FinishedDay + 4)
                    {
                        return true;
                    }

                    return false;
                })
                .WithCondition(() =>
                {
                    if (Global.EggplantCount.Value >= 30 && Global.BlueBerryCount.Value >= 20)
                    {
                        return true;
                    }

                    return false;
                })
                .WithFinishedAction((self) =>
                {
                    Global.EggplantCount.Value--;
                    Global.BlueBerryCount.Value--;

                    toolBarSystem.SubToolbarData("Eggplant", 30);
                    toolBarSystem.SubToolbarData("BlueBerry", 20);

                    Global.Coin.Value += 200;

                    var emailData = new EmailData()
                        .WithTitle($"{self.Publisher}第{self.FinishedDay}天的感谢信")
                        .WithPublisher(self.Publisher)
                        .WithInfo("工会对于上次的暴雨进行补救之后,派出了工作人员对我们进行了指导,现在稻米、土豆、萝卜等植物已经收获了不少。这也是您对我的支持,我也从一个老爷爷那里听到了你的事,他说你帮助了他很多。" +
                                  "还很喝得酒呢,只可惜我身体原因喝不了酒无法来感谢您对我的帮助。现在的田地的种植物都很好,看来没过多久就能靠自己生活下去了,到时候一定请您能来我家做客。")
                        .WithSendDay(self.FinishedDay + 1)
                        .WithRead(false);

                    emailSystem.AddEmail(emailData);
                    
                    MessagePanel.Default.AddMessage($"{self.Key}完成");
                });

            series.AddChallenge(firstChallenge)
                .AddChallenge(secondChallenge)
                .AddChallenge(thirdChallenge)
                .AddChallenge(fourthChallenge);
        }
    }
}